// Vue
import Vue from "vue";
import i18n from "./i18n";
import App from "./App";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import D2Crud from "@d2-projects/d2-crud";
import VCharts from "v-charts";
// 导出
import qs from "qs";
// 核心插件
import d2Admin from "@/plugin/d2admin";
// store
import store from "@/store/index";
//导出插件
// import pluginExport from "@d2-projects/vue-table-export";
// 导入xlsx
import XLSX from "xlsx";
// 时间格式化
import moment from "moment";
// 关闭当前路由
import closeing from "@/utils/closeing";
import fallback from "@/plugin/fallback"; // 引入
Vue.use(fallback); // 添加
Vue.prototype.closeing = closeing;
Vue.use(require("vue-moment"));
Vue.prototype.moment = moment;

// 菜单和路由设置
import router from "./router";
import { menuHeader, menuAside } from "@/menu";
import { frameInRoutes } from "@/router/routes";
import echarts from "echarts";
Vue.prototype.$echarts = echarts;
// 核心插件
Vue.use(d2Admin);
Vue.use(ElementUI);
Vue.use(D2Crud);
Vue.use(VCharts);
Vue.use(qs);
// Vue.use(pluginExport);
// console.log(store)
// console.log(store);
new Vue({
  router,
  store,
  i18n,
  render: h => h(App),
  created() {
    let routerTitle = frameInRoutes.concat(this.$store.getters.menus);
    // 处理路由 得到每一级的路由设置
    this.$store.commit("d2admin/page/init", routerTitle);
    // 设置顶栏菜单
    this.$store.commit("d2admin/menu/headerSet", menuHeader);
    // 设置侧边栏菜单
    this.$store.commit("d2admin/menu/asideSet", menuAside);
    // 初始化菜单搜索功能
    this.$store.commit("d2admin/search/init", menuHeader);
  },
  mounted() {
    // 展示系统信息
    this.$store.commit("d2admin/releases/versionShow");
    // 用户登录后从数据库加载一系列的设置
    this.$store.dispatch("d2admin/account/load");
    // 获取并记录用户 UA
    this.$store.commit("d2admin/ua/get");
    // 初始化全屏监听
    this.$store.dispatch("d2admin/fullscreen/listen");
  },
  watch: {
    // 检测路由变化切换侧边栏内容
    "$route.matched": {
      handler(matched) {
        if (matched.length > 0) {
          var work = this.$store.getters.menus;
          work.forEach(item => {
            if (item.children.length > 0) {
              item.children.forEach(i => {
                i.path = "/" + i.name;
                i.title = i.meta.title;
                i.icon = i.meta.icon;
              });
            }
          });
          function supplementPath(menu) {
            return menu.map(e => ({
              ...e,
              path: e.path || uniqueId("d2-menu-empty-"),
              ...(e.children
                ? {
                    children: supplementPath(e.children)
                  }
                : {})
            }));
          }
          const menuAside = supplementPath(work);
          const _side = menuAside.filter(menu => menu.path === matched[0].path);
          this.$store.commit(
            "d2admin/menu/asideSet",
            _side.length > 0 ? _side[0].children : []
          );
        }
      },
      immediate: true
    }
  }
}).$mount("#app");
Vue.filter("dateYMDHMSFormat", function(dateStr, pattern = "YYYY-MM-DD") {
  return moment(dateStr).format(pattern);
});
Vue.filter("dateYMDHMSFormat_ss", function(
  dateStr,
  pattern = "YYYY-MM-DD HH:mm:ss"
  // pattern = "MM-DD"
) {
  return moment(dateStr).format(pattern);
});
// 时间格式化
Vue.filter("dateYMDHMSFormat1", function(dateStr, pattern = "YYYY-MM-DD") {
  return moment(dateStr).format(pattern);
});
Vue.filter("dateYMDHMSFormat_ss1", function(
  dateStr,
  // pattern = "YYYY-MM-DD HH:mm:ss"
  pattern = "MM-DD"
) {
  return moment(dateStr).format(pattern);
});

Vue.prototype.dateNow = function getNowFormatTime() {
  var nowDate = new Date();
  var colon = ":";
  var h = nowDate.getHours();
  var m = nowDate.getMinutes();
  var s = nowDate.getSeconds();
  //补全0，并拼接
  return (
    getNowFormatDay(nowDate) +
    " " +
    completeDate(h) +
    colon +
    completeDate(m) +
    colon +
    completeDate(s)
  );
};

function getNowFormatDay(nowDate) {
  var char = "-";
  if (nowDate == null) {
    nowDate = new Date();
  }
  var day = nowDate.getDate();
  var month = nowDate.getMonth() + 1; //注意月份需要+1
  var year = nowDate.getFullYear();
  //补全0，并拼接
  return year + char + completeDate(month) + char + completeDate(day);
}

//补全0
function completeDate(value) {
  return value < 10 ? "0" + value : value;
}
